import type { FindLimitDto, FindList } from "./auth";
import type { Category } from "./category";
import type { Tags } from "./tag";
import type { Users } from "./user";

export interface Posts {
  id: string;
  title: string;
  content: string;
  contentHtml: string;
  summary: string;
  coverUrl: string;
  count: number;
  likeCount: number;
  isRecommend: boolean;
  status: string;
  author: Users;
  category: Category;
  tags: Tags[];
  publishTime: Date;
  createTime: Date;
  updateTime: Date;
}

export enum StatusPost {
  Publish = "publish",
  Draft = "draft",
}
export interface PostQueryFind extends FindLimitDto {
  status?: StatusPost;
  keyword?: string;
  title?: string;
  category?: number;
  tags?: number[];
}

export const findAll = (query?: PostQueryFind) => {
  return useServerRequest<FindList<Posts>>("/posts", {
    method: "GET",
    params: { ...query, status: StatusPost.Publish },
  });
};

export const findById = (id: string) => {
  return useServerRequest<Posts>(`/posts/${id}`, {
    method: "GET",
  });
};

export const findSearch = (query?: PostQueryFind) => {
  return useServerRequest<FindList<Posts>>(`/posts/search`, {
    method: "GET",
    params: { ...query },
  });
};

export interface postsArchive {
  createTime: Date;
  id: string;
  title: string;
}
export interface Archive {
  year: string;
  list: Array<postsArchive>;
}

export const findArchive = () => {
  return useServerRequest<Archive[]>(`/posts/getArchive`, {
    method: "GET",
  });
};
